/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sexypsf;

import java.io.File;
import java.io.FilenameFilter;

import android.app.Activity;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.os.Bundle;
import com.sexypsf.*;
import android.view.*;


class Mp3Filter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".psf"));
    }
}

public class MSexypsf extends Activity
{
	private static final String MEDIA_PATH = new String("/sdcard/");
	private static final int ID_EXIT = 1;
	
	private MAudioPlayerThread mineAudioPlayerThread;
	private TextView playerStatusView;
	private ListView musicListView;
	private ArrayAdapter<String> musicListAdapter;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        /** Create a TextView and set its content.
         * the text is retrieved by calling a native
         * function.
         */
        playerStatusView = new TextView(this);
        musicListView = new ListView(this);
        musicListAdapter = new ArrayAdapter<String>(this, R.layout.textview);
        musicListView.setAdapter(musicListAdapter);
    	File home = new File(MEDIA_PATH);
		if (home.listFiles( new Mp3Filter()).length > 0) {
    		for (File file : home.listFiles( new Mp3Filter())) {
    			musicListAdapter.add(file.getPath());
    		}
		}
		musicListView.setOnItemClickListener(new OnItemClickListener() {
			    public void onItemClick(AdapterView<?> parent, View view,
			        int position, long id) {
			      // When clicked, start playing
			    	String musicName = ((TextView) view).getText().toString();
			    	System.out.println("pick a music: " + musicName);
			    	playerStatusView.append("pick a music: " + musicName);
			        
			        mineAudioPlayerThread = new MAudioPlayerThread();
			        mineAudioPlayerThread.SetMusicName(musicName);
			        mineAudioPlayerThread.start();
			        setContentView(playerStatusView);
			    }
			  });
		setContentView(musicListView);
    }
    
    @Override
    public void onPause()
    {
    	playerStatusView.append("\n pause application");
    	super.onPause();
    }
    @Override
    public void onStop()
    {
    	playerStatusView.append("\n stop application");
    	super.onStop();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        boolean result = super.onCreateOptionsMenu(menu);
        menu.addSubMenu(0, ID_EXIT, 0, R.string.menu_exit);
        return result;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
    	switch(item.getItemId())
    	{
    	case ID_EXIT:
    		ExitApp();
    		return true;
    	}
        return super.onOptionsItemSelected(item);
    }
    
    public void ExitApp() {
    	playerStatusView.append("\n exit application");
    	playerStatusView.append("\nExit...");

    	if(mineAudioPlayerThread != null) {
	    	mineAudioPlayerThread.SetExit();
        	finish();
	    	try {
				mineAudioPlayerThread.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	else {
        	finish();
    	}
    }
    
}
